package ext.tianma.bom.eco;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import wt.change2.ChangeException2;
import wt.change2.ChangeHelper2;
import wt.change2.WTChangeActivity2;
import wt.change2.WTChangeOrder2;
import wt.fc.ObjectReference;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.collections.WTArrayList;
import wt.fc.collections.WTList;
import wt.folder.Folder;
import wt.folder.FolderHelper;
import wt.inf.container.WTContainer;
import wt.log4j.LogR;
import wt.method.MethodContext;
import wt.part.WTPart;
import wt.pom.Transaction;
import wt.pom.WTConnection;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.type.TypeDefinitionReference;
import wt.type.TypedUtility;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.vc.VersionControlHelper;
import wt.vc.wip.WorkInProgressHelper;

import com.ptc.core.foundation.type.server.impl.TypeHelper;
import com.ptc.core.meta.common.TypeIdentifier;

import ext.com.workflow.WorkflowUtil;
import ext.tianma.bom.bean.BOMBean;
import ext.tianma.bom.bean.ECOBean;
import ext.tianma.bom.service.TMECOUtil;
import ext.tianma.bom.util.ChangeUtil;
import ext.tianma.bom.util.GenericUtil;
import ext.tianma.bom.util.IBAUtil;
import ext.tianma.model.TMECOITEMModel;
import ext.tianma.model.util.TMObjectUtilHelper;
import ext.tianma.number.util.PartUtil;
import ext.tianma.util.FolderUtil;

/**
 * ECO清单增删改查
 * 
 * @author Administrator
 * 
 */

public class ECOCRUDUtil {
	private static final Logger log = LogR.getLogger(ECOCRUDUtil.class.getName());
	
	public static String OPEN = "open";
	public static String CLOSE = "close";
	public static String First = "1";// 批量变更
	public static String Zero = "0";// 单个变更
	private static final String NEW_PRODUCT_MODIFY = "NewProductModify";
	private static final String MASS_PRODUCT_MODIFY = "MassProductionReleaseModify";
	private static final String INWORK = "INWORK";
	private static final String APPROVE_MODIFICATIONS = "APPROVE_MODIFICATIONS";
	private static final String NEW_PRODUCT = "NewProduct";//新品
	private static final String MASS_RELEASE = "MassProductionRelease";//量产发布
	private static final String APPROVEL = "APPROVAL";

	private static final String RELEASED = "RELEASED";

	// 试产
	private static final String TRIAL_PRODUCTION = "TrialProduction";
	// 小批量
	private static final String CUSTOMER_SAMPLE = "CustomerSample";
	// 风险批量
	private static final String RISK_ORDER = "RiskOrder";
	// 试产修改
	private static final String TRIAL_PRODUCTION_MODIFY = "TrialProductionModify";
	// 小批量修改
	private static final String CUSTOMER_SAMPLE_MODIFY = "CustomerSampleModify";
	// 风险批量修改
	private static final String RISK_ORDER_MODIFY = "RiskOrderModify";

	// OBSOLESCENCE 废弃
	private static final String OBSOLESCENCE = "OBSOLESCENCE";

	// NewPartModify 新物料修改
	private static final String NewPartModify = "NewPartModify";

	/** 模板路径 */
	private static String templateFolder = null;
	private static String tempFolderPath;

	static {
		try {
			templateFolder = WTProperties.getLocalProperties().getProperty("wt.home");
			tempFolderPath = templateFolder + File.separator + "codebase" + File.separator + "ext" + File.separator
					+ "tianma" + File.separator + "part" + File.separator + "config" + File.separator + "ecostate.xlsx";
			File tempFolder = new File(tempFolderPath);
			if (!tempFolder.exists() || !tempFolder.isDirectory()) {
				tempFolder.mkdir();
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * ECO清单创建
	 */
	public static TMECOITEMModel createECO(BOMBean bean) {
		TMECOITEMModel item = null;
		String strObjType = TMECOITEMModel.class.getName();
		log.debug("strObjType:" + strObjType);
		TypeDefinitionReference typeDefinitionRef = TypedUtility.getTypeDefinitionReference(strObjType);
		TypeIdentifier typeidentifier = TypeHelper.getTypeIdentifier(strObjType);
		log.debug("==============createECO:" + bean.toString() + "--------type:" + bean.getType());
		/*
		 * try { WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
		 * .getReference(bean.getEcoid()).getObject(); if(eco!=null){ //避免重复提交
		 * 先删除后新增 deleteECOData(eco.getNumber(),bean.getEffectnumber()); } }
		 * catch (WTException e) { log.debug(
		 * "==============delete failed===="+e); }
		 */

		// 不同操作类型设置的列不同
		if (BOMBean.ADDPart.equals(bean.getType())) {
			log.debug("==============type ADDPart:" + bean.getType());
			// 1
			ADDPart(item, bean);
		} else if (BOMBean.DelPart.equals(bean.getType())) {
			// 1
			DelPart(item, bean);
		} else if (BOMBean.ReplacePart.equals(bean.getType())) {
			// 1
			ReplacePart(item, bean);
		} else if (BOMBean.UpdatePart.equals(bean.getType())) {
			// 1
			UpdatePart(item, bean);
		} else if (BOMBean.ADDReplcePart.equals(bean.getType())) {
			// 1
			ADDReplcePart(item, bean);
		} else if (BOMBean.DelReplcePart.equals(bean.getType())) {
			// 1
			DelReplcePart(item, bean);
		} else if (BOMBean.UpdateReplcePart.equals(bean.getType())) {
			// 1
			UpdateReplcePart(item, bean);
		} else if (BOMBean.UpdateAPart.equals(bean.getType())) {
			// 1
			UpdateAPart(item, bean);
		} else if (BOMBean.ReplaceABPart.equals(bean.getType())) {
			// 1
			ReplaceABPart(item, bean);
		} 
		//add by xucr on 20210125 替代料用量修改
		else if(BOMBean.UpdateSubstitutesQuantity.equals(bean.getType())) {
			addSubstitutes(item, bean);
		}
		return item;
	}

	/**
	 * 新增物料时创建ECO清单
	 * 
	 * @param item
	 * @param bean
	 * @return
	 */
	public static TMECOITEMModel ADDPart(TMECOITEMModel item, BOMBean bean) {
		log.debug("============ADDPart Start=========");
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			log.debug("=========eco number:" + eco.getNumber());
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */

			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());
			item.setECOOperatingType(bean.getType());
			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOPartNumber(bean.getOldpartnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartamount())) {
				item.setECODosage(bean.getOldpartamount());
			}
			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());
			// WTContainer con = getContainerByName("变更库");
			// item.setContainer(con);
			// item.setOrganization(con.getContainer().getOrganization());
			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			log.debug("============ADDPart catch=========" + e.getMessage());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		log.debug("============ADDPart end=========" + item);
		return item;
	}

	public static TMECOITEMModel DelPart(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(number);
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());
			item.setECOOperatingType(bean.getType());
			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOPartNumber(bean.getOldpartnumber());
			// item.setECODosage(Long.parseLong(bean.getOldpartamount()));
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartlinenumber())) {
				item.setECOLineNumber(bean.getOldpartlinenumber());
			}
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartamount())) {
				item.setECODosage(bean.getOldpartamount());
			}
			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());
			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}

	public static TMECOITEMModel ReplacePart(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());
			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOPartNumber(bean.getOldpartnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartamount())) {
				item.setECODosage(bean.getOldpartamount());
			}
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartlinenumber())) {
				item.setECOLineNumber(bean.getOldpartlinenumber());
			}
			item.setECONewPartNumber(bean.getNewpartnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getNewpartamount())) {
				item.setECONewDosage(bean.getNewpartamount());
			}
			if (!BOMHelp.isNullOrEmpty(bean.getNewpartlinenumber())) {
				item.setECONewLineNumber(bean.getNewpartlinenumber());
			}
			item.setECOOperatingType(bean.getType());
			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());

			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}

	public static TMECOITEMModel UpdatePart(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());
			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOPartNumber(bean.getOldpartnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartamount())) {
				item.setECODosage(bean.getOldpartamount());
			}

			if (!BOMHelp.isNullOrEmpty(bean.getOldpartlinenumber())) {
				item.setECOLineNumber(bean.getOldpartlinenumber());
			}
			if (!BOMHelp.isNullOrEmpty(bean.getNewpartamount())) {
				item.setECONewDosage(bean.getNewpartamount());
			}
			item.setECOOperatingType(BOMBean.UpdatePart);
			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());

			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}

	public static TMECOITEMModel ADDReplcePart(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());

			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOOperatingType(BOMBean.ADDReplcePart);
			item.setECOPartNumber(bean.getPartAnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getPartAlinenumber())) {
				item.setECOLineNumber(bean.getPartAlinenumber());
			}
			item.setECOSubstituteNumber(bean.getPartBnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getPartBamount())) {
				item.setECOSubstituteDosage(bean.getPartBamount());
			}
			/*
			 * item.setECOSubstituteLineNmuber(Long.parseLong(bean
			 * .getPartBlinenumber()));
			 */
			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());

			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}

	public static TMECOITEMModel DelReplcePart(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());

			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOPartNumber(bean.getPartAnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getPartAlinenumber())) {
				item.setECOLineNumber(bean.getPartAlinenumber());
			}
			item.setECOSubstituteNumber(bean.getPartBnumber());
			item.setECOOperatingType(BOMBean.DelReplcePart);
			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());
			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}

	public static TMECOITEMModel UpdateReplcePart(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());
			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOOperatingType(BOMBean.UpdateReplcePart);
			item.setECOPartNumber(bean.getPartAnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getPartAlinenumber())) {
				item.setECOLineNumber(bean.getPartAlinenumber());
			}
			item.setECOSubstituteNumber(bean.getOldpartnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartamount())) {
				item.setECOSubstituteDosage(bean.getOldpartamount());
			}
			item.setECONewSubstituteNumber(bean.getNewpartnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getNewpartamount())) {
				item.setECONewSubstituteDosage(bean.getNewpartamount());
			}

			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());
			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}

	public static TMECOITEMModel UpdateAPart(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());
			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOOperatingType(BOMBean.UpdateAPart);
			item.setECOPartNumber(bean.getOldpartnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartamount())) {
				item.setECODosage(bean.getOldpartamount());
			}
			if (!BOMHelp.isNullOrEmpty(bean.getOldpartlinenumber())) {
				item.setECOLineNumber(bean.getOldpartlinenumber());
			}
			item.setECONewPartNumber(bean.getNewpartnumber());
			if (!BOMHelp.isNullOrEmpty(bean.getNewpartamount())) {
				item.setECONewDosage(bean.getNewpartamount());
			}

			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());
			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}

	public static TMECOITEMModel ReplaceABPart(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			item.setECONumber(eco.getNumber());
			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOOperatingType(BOMBean.ReplaceABPart);
			item.setECOPartNumber(bean.getOldpartnumber());
			if (bean.getOldpartlinenumber() != null && !"".equals(bean.getOldpartlinenumber()) && !"undefined".equals(bean.getOldpartlinenumber())) {
				item.setECOLineNumber(bean.getOldpartlinenumber());
			}
			item.setECONewPartNumber(bean.getNewpartnumber());
			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());

			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}
	
	/**
	 * 替代料用量修改
	 * @param item
	 * @param bean
	 * @return
	 * @author xucr
	 * @CreateDate 2021年1月25日
	 */
	public static TMECOITEMModel addSubstitutes(TMECOITEMModel item, BOMBean bean) {
		// ECO OID
		String oid = bean.getEcoid();
		try {
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			WTPart part = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			item = TMECOITEMModel.newTMECOITEMModel();
			String number = TMObjectUtilHelper.getTMECOITEMAutoNumber();
			/**
			 * 设置对象属性
			 */
			item.setNumber(number);
			item.setName(eco.getNumber());
			// 受影响对象
			item.setECOAffectedRef(ObjectReference.newObjectReference(part));
			// ECO
			item.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			bean.setEcoid(oid);
			item.setECOSubstituteDosage(bean.getSubOldAmount()); //替代料用量
			item.setECONewSubstituteDosage(bean.getSubNewAmount());//新替代料用量，暂存替代料新用量
			item.setECONumber(eco.getNumber());
			item.setStr1(bean.getEffectnumber());
			item.setECOView(bean.getView());
			item.setECOOperatingType(BOMBean.UpdateSubstitutesQuantity);
			item.setECOSubstituteNumber(bean.getSubstitutesNum());//替代料料号
			item.setECOPartNumber(bean.getMainNum());//物料号，主料号
			if (StringUtils.isNotEmpty(bean.getMainLineNumber())) {
				item.setECOLineNumber(bean.getMainLineNumber());//主料行号
			}
			item.setECOStatus(OPEN);
			// 0 单个变更 1 批量变更
			item.setStr2(bean.getChangetype());

			item.setContainerReference(eco.getContainerReference());
			Folder folder = FolderUtil.getFolder("/Default", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(item);
				FolderHelper.assignLocation(documentsList, folder);
			} else {

			}
			item = (TMECOITEMModel) PersistenceHelper.manager.save(item);
			PersistenceHelper.manager.refresh(item);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return item;
	}

	/**
	 * 选择筛选条件：ECO编号、ECONumber 成品编号、str1 成品名称、 成品视图、ECOView 变更类型；
	 * 
	 * 创建时间 createstampa2
	 * 
	 * ECOOperatingType
	 * 
	 * @param wtConn
	 * @param bean
	 * @return
	 */
	public static List<ECOBean> queryECOOrder(String key, String value, String startdate, String enddate) {
		log.debug("========queryECOOrder  key====" + key + "===value:" + value);
		MethodContext context = MethodContext.getContext();
		WTConnection conn = null;
		Transaction trans = new Transaction();
		boolean result = false;
		List<ECOBean> list = new ArrayList<ECOBean>();
		List<String> listStr1 = new ArrayList<String>();
		StringBuffer sb = new StringBuffer();
		// String querySQL =
		// "select * from TMECOITEMModel where ECONumber like ? or ECOPARTNUMBER
		// like ? or ECOView like ? or ECOOperatingType like ? ";
		// ,str1,ecoview,ECOOperatingType
		sb.append("select distinct econumber,createstampa2 from TMECOITEMModel");
		if (!BOMHelp.isNullOrEmpty(startdate)) {
			sb.append("  where createstampa2 >=");
			sb.append(" TO_DATE('" + startdate + "','yyyy/MM/dd hh24:mi:ss')");
			if(!BOMHelp.isNullOrEmpty(enddate)){
				sb.append(" and createstampa2 <= TO_DATE('" + enddate + "','yyyy/MM/dd hh24:mi:ss')");

			}
			if (!BOMHelp.isNullOrEmpty(value) && !BOMHelp.isNullOrEmpty(key) && !"creator".equals(key)) {
				sb.append(" and " + key + "  like '%" + value + "%'");
			}

		}else if(!BOMHelp.isNullOrEmpty(enddate)){
			sb.append(" where createstampa2 <= TO_DATE('" + enddate + "','yyyy/MM/dd hh24:mi:ss')");
			if (!BOMHelp.isNullOrEmpty(value) && !BOMHelp.isNullOrEmpty(key) && !"creator".equals(key)) {
				sb.append(" and " + key + "  like '%" + value + "%'");
			}
		}else{
			if (!BOMHelp.isNullOrEmpty(value) && !BOMHelp.isNullOrEmpty(key) && !"creator".equals(key)) {
				sb.append(" where " + key + "  like '%" + value + "%'");
			}
		}
		
		try {
			conn = (WTConnection) context.getConnection();
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			pstmt = conn.prepareStatement(sb.toString());
			if (!BOMHelp.isNullOrEmpty(value) && !BOMHelp.isNullOrEmpty(key) && !"creator".equals(key)) {
				// pstmt.setString(1, "'%" + value + "%'");
				log.debug("========queryECOOrder  key====" + key + "===value:" + value);
			}

			System.out.println("========SQL====" + sb.toString());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				ECOBean bean = new ECOBean();
				String ECONumber = rs.getString("ECONumber");
				// ECO单号
				bean.setECONumber(ECONumber);
				// 成品号
				/*
				 * String ECOPARTNUMBER = rs.getString("ECOPARTNUMBER");
				 * bean.setECOPartNumber(ECOPARTNUMBER);
				 */
				// 受影响对象编号
				/*
				 * String effectnumber = rs.getString("str1");
				 * bean.setECOAffectnumber(effectnumber); // 视图 String view =
				 * rs.getString("ECOView"); bean.setECOView(view); // 变更类型
				 * String ECOOperatingType = rs.getString("ECOOperatingType");
				 * bean.setECOOperatingType(ECOOperatingType);
				 */
				// 创建时间
				Timestamp createtime = rs.getTimestamp("createstampa2");
				bean.setCreatetime(IBAUtil.transfTime(createtime));
				if (!listStr1.contains(ECONumber)) {
					list.add(bean);
					listStr1.add(ECONumber);
				}
				result = true;
			}
			rs.close();
			rs = null;

			pstmt.close();
			pstmt = null;
			conn.release();
			conn = null;
			// result = true;
		} catch (Exception e) {
			result = false;
		} finally {
			if (conn != null) {
				conn.release();
			}
		}
		return list;
	}

	/**
	 * 流程审阅通过后ECO变更
	 * 
	 * 根据ECO number 取本次变更数据进行逻辑处理
	 * 
	 * @param oid
	 */
	public static String commitECO(String oid) {
		boolean flag = false;
		String result = "";
		WTChangeOrder2 eco = null;
		try {
			eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oid).getObject();
			String status = TMECOUtil.correctStatus;
			// 变更 逻辑
			List<BOMBean> lists = queryECOByNumber(eco.getNumber());
			for (BOMBean bean : lists) {
				if (BOMBean.ADDPart.equals(bean.getType())) {
					// 新增物料
					result += BOMECOUpdate.addPartToBom(bean, BOMECOUpdate.Commit);
				} else if (BOMBean.DelPart.equals(bean.getType())) {
					// 删除物料
					result += BOMECOUpdate.deletePartBom(bean, BOMECOUpdate.Commit);
				} else if (BOMBean.ReplacePart.equals(bean.getType())) {
					// 替换物料
					// 1.先删除原料
					result += BOMECOUpdate.deletePartBom(bean, BOMECOUpdate.Commit);
					bean.setOldpartnumber(bean.getNewpartnumber());
					bean.setOldpartamount(bean.getNewpartamount());
					// 2. 新增新料
					result += BOMECOUpdate.addPartToBom(bean, BOMECOUpdate.Commit);
				} else if (BOMBean.UpdatePart.equals(bean.getType())) {
					// 更换用量
					result += BOMECOUpdate.updateBom(bean, BOMECOUpdate.Commit);
				} else if (BOMBean.ADDReplcePart.equals(bean.getType())) {
					// 新增替代料
					result += BOMECOUpdate.addReplacePartToBom(bean, BOMECOUpdate.Commit);
				} else if (BOMBean.DelReplcePart.equals(bean.getType())) {
					// 删除替代料
					result += BOMECOUpdate.deleteReplacePartToBom(bean, BOMECOUpdate.Commit);
				} else if (BOMBean.UpdateReplcePart.equals(bean.getType())) {
					String effect = bean.getEffectnumber();
					String partBNumber = bean.getNewpartnumber();
					String partANumber = bean.getOldpartnumber();
					// 更换替代料
					// 先删除原替代料
					result += BOMECOUpdate.deleteOldReplacePartBToBom(bean, BOMECOUpdate.Commit);
					// 更换新替代料
					// BOMECOUpdate.addNewReplacePartBToBom(bean,BOMECOUpdate.Commit);

				} else if (BOMBean.UpdateAPart.equals(bean.getType())) {
					// 更换主料
					// 先删除原主料与受影响对象的关系
					// deleteOldPartAndEffectRelation(bean);
					String oldnumber = bean.getOldpartnumber();
					String oldamout = bean.getOldpartamount();
					String newnumber = bean.getNewpartnumber();
					String newamout = bean.getNewpartamount();

					// 返回原主料的替代关系编号和原主料编号集合
					// Map<String,String> subnumberlist =
					// BOMECOUpdate.deletePartBom2(bean, BOMECOUpdate.Commit);
					Map<String, String> subnumberlist = BOMECOUpdate.deletePartBom2(bean, BOMECOUpdate.Commit);
					bean.setOldpartnumber(bean.getNewpartnumber());
					bean.setOldpartamount(bean.getNewpartamount());
					bean.setNewpartnumber(oldnumber);
					bean.setNewpartamount(oldamout);
					// 2. 新增新料
					result += BOMECOUpdate.addPartToBom2(bean, BOMECOUpdate.Commit, subnumberlist);
					// 更换新替代料
					// BOMECOUpdate.addNewPartToBom(bean, BOMECOUpdate.Commit);
				} else if (BOMBean.ReplaceABPart.equals(bean.getType())) {
					// 主次替换
					Map<String, String> partlist = new HashMap<String, String>();
					String effectNumber = bean.getEffectnumber();
					String partBNumber = bean.getNewpartnumber();
					String partANumber = bean.getOldpartnumber();
					String view = bean.getView();
					// BOMECOUpdate.deletePartBom2(bean, BOMECOUpdate.Commit);
					Map maps = BOMECOUpdate.deletePartBom3(bean, BOMECOUpdate.Commit, partlist);
					if (maps.get("bean") != null && maps.get("bean") instanceof BOMBean) {
						bean = (BOMBean) maps.get("bean");
					}
					if (maps.get("sublist") != null) {
						partlist = (Map<String, String>) maps.get("sublist");
					}
					if(maps.containsKey("errorlog") && maps.get("errorlog") != null){
						result += maps.get("errorlog");
					}
					String oldamout = bean.getOldpartamount();
					String newamout = bean.getNewpartamount();
					bean.setOldpartnumber(bean.getNewpartnumber());
					bean.setOldpartamount(newamout);
					bean.setNewpartnumber(partANumber);
					bean.setNewpartamount(oldamout);
					// 2. 新增新料
					result += BOMECOUpdate.addPartToBom3(bean, BOMECOUpdate.Commit, partlist);

				}
			}

			// 升版受影响对象
			List<BOMBean> list = queryEffectPartByECONumber(eco.getNumber(), "");

			// 升版后的对象 添加至产生的对象中
			List<WTPart> partlist = new ArrayList<WTPart>();
			
			//先取Excel中 不同类型对应的状态及优先级
			Map<String, Map<String, String>> map = readExcel();
			
			for (BOMBean bean : list) {
				WTPart part = BOMHelp.getPart(bean.getEffectnumber(), "", bean.getView());
				if(WorkInProgressHelper.isCheckedOut(part)){
					System.out.println("=====commitECO part isCheckedOut:"+part);
					part = (WTPart)GenericUtil.checkin(part);
				}
				System.out.println("=====commitECO part:"+part);
				String partstate = part.getState().getState().getDisplay(Locale.CHINA);
				String partEnState = part.getState().getState().toString();
				log.debug("====effect number:"+bean.getEffectnumber()+"===effect state:"+partstate);
				part = BOMHelp.getNewVersionDoc(part);
				log.debug("======改版后 part version:"
						+ VersionControlHelper.getIterationDisplayIdentifier(part).toString());
				partlist.add(part);
				
				//设置受影响对象状态      成品/半成品/原材料
				changePartState(part,partEnState,map);
			}

			List<BOMBean> bomlist = queryECOByNumber(eco.getNumber());
			//处理问题,替代料用量修改,在修订前版本生效.
			//把该代码移动到,修订后执行
			for (BOMBean bean : bomlist) {
			//add by xucr on 20210120 添加新的菜单：替代料用量修改
				 if(BOMBean.UpdateSubstitutesQuantity.equals(bean.getType())) {
					 result += BOMECOUpdate.UpdateSubstitutesQuantity(bean);
				 }
			}

			// 添加至产生的对象中
			addToECOAfterData(eco, partlist);

			// 修改本次变更物料的状态
			changeState(lists,map);

		} catch (Exception e) {
			result +=  e.getMessage();
			log.debug("===========commit failed=========" + e);
			flag = false;
			e.printStackTrace();
		}finally{
			// 更新数据库 字段 ECOStatus
			if(eco!=null){
				flag = commitECOData(eco.getNumber());
			}
		}
		return result;
	}

	/**
	 * 添加变更后的受影响对象到 产生对象
	 * 
	 * @param eco
	 * @param partlist
	 */
	public static void addToECOAfterData(WTChangeOrder2 eco, List<WTPart> partlist) {
		log.debug("================addToECOAfterData start============"+partlist.size());
		WTArrayList erpObjList = new WTArrayList();
		try {
			QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						ChangeUtil.addChangeableAfterToChangeNotice(eco, partlist, eca);
					}
				}
			}
		} catch (ChangeException2 e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
		log.debug("================addToECOAfterData end============");
	}

	// 同步变更物料的状态
	public static void changeState(List<BOMBean> lists,Map<String, Map<String, String>> map) {
		
		//key:物料（原材料，半成品、成品）    value:受影响对象 （成品、半成品）
		Map<WTPart, WTPart> SemiFinishedGoodmap = new HashMap<WTPart, WTPart>();
		
		//key:物料（原材料，半成品、成品）    value:受影响对象 （成品、半成品）集合
		Map<WTPart, ArrayList<WTPart>> SemiFinishedGoodmapsss = new HashMap<WTPart, ArrayList<WTPart>>();
		
		// 变更数据的集合（不包含删除物料 及被替换料）
		List<WTPart> partlists = new ArrayList<WTPart>();
		WTPart part = null;
		WTPart effectpart = null;
		for (BOMBean bean : lists) {
			try {
				if (BOMBean.ADDPart.equals(bean.getType())) {
					// 新增物料
					// 新料
					part = GenericUtil.getPart(bean.getOldpartnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					// 受影响对象
					effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					/*if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
					
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					//添加受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
					
				} else if (BOMBean.DelPart.equals(bean.getType())) {
					// 删除物料
					// 受影响对象
					/*effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
				} else if (BOMBean.ReplacePart.equals(bean.getType())) {
					// 替换物料
					// 新料
					part = GenericUtil.getPart(bean.getNewpartnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					// 受影响对象
					effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					/*if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
					
					
					//1
					//添加新料受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
					
					
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
				} else if (BOMBean.UpdatePart.equals(bean.getType())) {
					// 更换用量
					// 原料
					part = GenericUtil.getPart(bean.getOldpartnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					// 受影响对象
					effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					/*if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
					
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					
					//添加原料受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
				} else if (BOMBean.ADDReplcePart.equals(bean.getType())) {
					// 新增替代料

					// 主料
					part = GenericUtil.getPart(bean.getPartAnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					// 受影响对象
					effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					/*if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
					
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					//添加主料至受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
					
					// 替代料
					part = GenericUtil.getPart(bean.getPartBnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					//添加替代料至受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
				} else if (BOMBean.DelReplcePart.equals(bean.getType())) {
					// 删除替代料
					// 主料
					part = GenericUtil.getPart(bean.getPartAnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					// 受影响对象
					effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					/*if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
					
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					//添加主料受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
				} else if (BOMBean.UpdateReplcePart.equals(bean.getType())) {
					// 主料
					part = GenericUtil.getPart(bean.getPartAnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					
					// 受影响对象
					effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					/*if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
					//主料 ----受影响对象
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					//添加主料至受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
					
					// 新替代料
					part = GenericUtil.getPart(bean.getNewpartnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					
					//新替代料 ----受影响对象
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					//添加新替代料至受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
				} else if (BOMBean.UpdateAPart.equals(bean.getType())) {
					// 更换主料

					// 原主料
					part = GenericUtil.getPart(bean.getOldpartnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					
					// 受影响对象
					effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					/*if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
					//原主料 --受影响对象
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					//添加原主料至受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
					

					// 新主料 作为替换件
					part = GenericUtil.getPart(bean.getNewpartnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					//替换件 --受影响对象
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					//添加替换件至受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}

				} else if (BOMBean.ReplaceABPart.equals(bean.getType())) {
					// 主次替换
					// 原主料
					part = GenericUtil.getPart(bean.getOldpartnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					// 受影响对象
					effectpart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
					/*if (effectpart != null && !partlists.contains(effectpart)) {
						partlists.add(effectpart);
					}*/
					//原主料 --受影响对象
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					//添加原主料至受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
					
					// 新主料
					part = GenericUtil.getPart(bean.getNewpartnumber(), "", bean.getView());
					if (part != null && !partlists.contains(part)) {
						partlists.add(part);
					}
					
					// 新主料 --受影响对象
					if(part != null && effectpart!=null && !SemiFinishedGoodmap.containsKey(part)){
						SemiFinishedGoodmap.put(part, effectpart);
					}
					
					//添加新主料至受影响对象集合
					if(part != null && effectpart!=null && !SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = new ArrayList<WTPart>();
						list.add(effectpart);
						SemiFinishedGoodmapsss.put(part, list);
					}else if(part != null && effectpart!=null && SemiFinishedGoodmapsss.containsKey(part)){
						ArrayList<WTPart> list = SemiFinishedGoodmapsss.get(part);
						if(!list.contains(effectpart)){
							list.add(effectpart);
							SemiFinishedGoodmapsss.put(part, list);
						}
						
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		changePartsState(partlists,map,SemiFinishedGoodmap,SemiFinishedGoodmapsss);
	}
	/**
	 * 针对变更物料做处理
	 * @param partlists
	 * @param map
	 */
	public static void changePartsState(List<WTPart> partlists,Map<String, Map<String, String>> map,Map<WTPart, WTPart> fsmap,Map<WTPart, ArrayList<WTPart>> SemiFinishedGoodmapsss) {
		log.debug("=====================changePartsState start===========");
		//半成品
		Map<String, String> sheet1map = new HashMap<String, String>();
		//成品
		Map<String, String> sheet2map = new HashMap<String, String>();
		//原材料
		Map<String, String> sheet3map = new HashMap<String, String>();
		Iterator iter = map.keySet().iterator();
		while(iter.hasNext()){
			String key = (String)iter.next();
			Map<String, String> sheet = map.get(key);
			log.debug("key:"+key);
			log.debug("value:"+sheet.toString());
			if("sheet1".equals(key)){
				sheet1map = map.get(key);
			}else if("sheet2".equals(key)){
				sheet2map = map.get(key);
			}else if("sheet3".equals(key)){
				sheet3map = map.get(key);
			}
		}
		log.debug("====sheet3map"+sheet3map.toString());
		log.debug("====sheet2map"+sheet2map.toString());
		log.debug("====sheet1map"+sheet1map.toString());
		log.debug("===changePartsState  partlists size:"+partlists.size());
		for (WTPart part : partlists) {
			log.debug("===changePartsState  partlists:"+part.getNumber());
			try {
				String softtype = GenericUtil.getTypeId(part);
				String partstate = part.getState().getState().getDisplay(Locale.CHINA);
				if(softtype.indexOf("cn.tianma.FinishedGood") != -1){
					/*//成品状态不变
					if(!partstate.equals(state)){
						WorkflowUtil.setLifeCycleState(part, state);
					}*/
					
				}else if(softtype.indexOf("cn.tianma.SemiFinishedGood") != -1){
					log.debug("===cn.tianma.SemiFinishedGood==="+part.getNumber()+"===partstate:"+partstate);
					//半成品 z状态在配置表中
					if(sheet1map.containsKey(partstate)){
						//优先级
						double standardlevol = Double.parseDouble(sheet1map.get("新品"));
						//当前物料优先级
						double statelevol = Double.parseDouble(sheet1map.get(partstate));
						log.debug("=============changePartsState SemiFinishedGood 222 statelevol<=standardlevol:"+(statelevol<=standardlevol));
						/*if(statelevol<standardlevol){
							log.debug("===cn.tianma.SemiFinishedGood levol==="+statelevol+":"+standardlevol);
							//优先级小于新品 
							WorkflowUtil.setLifeCycleState(part, NEW_PRODUCT);
						}else{*/
							//比较成品状态  再 设置
							log.debug("=============changePartsState SemiFinishedGood containsKey:"+fsmap.containsKey(part));
							if(SemiFinishedGoodmapsss.containsKey(part)){
								ArrayList<WTPart> finishlist = SemiFinishedGoodmapsss.get(part);
								System.out.println("==part number:"+part.getNumber()+"part view:"+part.getViewName()+"===finishlist size:"+finishlist.size());
								double finishlevolMax = 0;
								String finalstate = "";
								//取成品的最高优先级状态
								for(WTPart partA : finishlist){
									System.out.println("===PartA number"+partA.getNumber()+"==partA view:"+partA.getViewName());
									if(partA != null && partA.getViewName().equals(part.getViewName())){
										log.debug("===cn.tianma.SemiFinishedGood partA number==="+partA.getNumber());
										String softtypeA = GenericUtil.getTypeId(partA);
										log.debug("===cn.tianma.SemiFinishedGood softtypeA==="+softtypeA);
										String partAstate = partA.getState().getState().getDisplay(Locale.CHINA);
										String partAstateEN = partA.getState().getState().toString();
										//取成品时的最高优先级状态
										if((softtypeA.indexOf("cn.tianma.FinishedGood") != -1 && sheet2map.containsKey(partAstate))
												||(softtypeA.indexOf("cn.tianma.SemiFinishedGood") != -1 && sheet1map.containsKey(partAstate))){
											double stateAlevol = Double.parseDouble(sheet2map.get(partAstate));
											log.debug("===cn.tianma.SemiFinishedGood levol 4==="+stateAlevol+":"+statelevol);
											if(stateAlevol>finishlevolMax){
												//成品 状态优先级大于半成品当前状态优先级  设置为成品的状态
												finishlevolMax = stateAlevol;
												finalstate = partAstateEN;
												//WorkflowUtil.setLifeCycleState(part, partAstateEN);
											}
										}
										
										
									}
								}
								System.out.println("===finishlevolMax:"+finishlevolMax);
								//成品状态高的 则设置为成品的状态
								//成品状态优先级低于 新品时  半成品状态设置为新品
								if(!"0.0".equals(finishlevolMax)){
									if(finishlevolMax<=standardlevol){
										System.out.println("=====SemiFinishedGood 111=====");
										part = (WTPart)WorkflowUtil.setLifeCycleState(part, NEW_PRODUCT);
										changeChildState(sheet1map,sheet2map,sheet3map,part);
									}else if(finishlevolMax>statelevol){
										System.out.println("=====SemiFinishedGood 222=====");
										part = (WTPart)WorkflowUtil.setLifeCycleState(part, finalstate);
										changeChildState(sheet1map,sheet2map,sheet3map,part);
									}
								}
								
							}
						}
						
				}else if(softtype.indexOf("cn.tianma.RawMaterial") != -1){
					//原材料
					if(sheet3map.containsKey(partstate)){
						//优先级  标准取成品的状态优先级  与成品的优先级有关
						double standardlevol = Double.parseDouble(sheet2map.get("新品"));
						//当前物料优先级
						double statelevol = Double.parseDouble(sheet3map.get(partstate));
						log.debug("===cn.tianma.RawMaterial levol 1==="+standardlevol+":"+statelevol);
						log.debug("===cn.tianma.RawMaterial levol 2==="+standardlevol+":"+statelevol);
						log.debug("=============changePartsState RawMaterial 222 statelevol<=standardlevol:"+(statelevol<=standardlevol));
						/*if(statelevol<=standardlevol){
							log.debug("=============changePartsState RawMaterial 1111=====");

							//正在工作、新品时
							WorkflowUtil.setLifeCycleState(part, NEW_PRODUCT);
						}else{
							log.debug("=============changePartsState RawMaterial 22222=====");

							//非正在工作、新品时  设置为  量产发布
							WorkflowUtil.setLifeCycleState(part, MASS_RELEASE);
						}*/
						if(SemiFinishedGoodmapsss.containsKey(part)){
							ArrayList<WTPart> finishlist = SemiFinishedGoodmapsss.get(part);
							double finishlevolMax = 0;
							String finalstate = "";
							//取成品的最高优先级状态
							for(WTPart partA : finishlist){
								if(partA != null){
									log.debug("===cn.tianma.RawMaterial partA number==="+partA.getNumber());
									String softtypeA = GenericUtil.getTypeId(partA);
									log.debug("===cn.tianma.RawMaterial softtypeA==="+softtypeA);
									String partAstate = partA.getState().getState().getDisplay(Locale.CHINA);
									String partAstateEN = partA.getState().getState().toString();
									//取成品时的最高优先级状态
									if((softtypeA.indexOf("cn.tianma.FinishedGood") != -1
											&& sheet2map.containsKey(partAstate))
											||
											(softtypeA.indexOf("cn.tianma.SemiFinishedGood") != -1
											&& sheet1map.containsKey(partAstate))
											){
										double stateAlevol = 0;
										if(softtypeA.indexOf("cn.tianma.FinishedGood") != -1){
											//受影响对象 成品时  取 成品sheet
											stateAlevol = Double.parseDouble(sheet2map.get(partAstate));
										}else{
											//受影响对象 半成品时  取 半成品sheet
											stateAlevol = Double.parseDouble(sheet1map.get(partAstate));
										}
										
										log.debug("===cn.tianma.RawMaterial levol 4==="+stateAlevol+":"+statelevol);
										if(stateAlevol>finishlevolMax){
											//取 成品或半成品  状态的最高优先级
											finishlevolMax = stateAlevol;
											finalstate = partAstateEN;
										}
									}
								}
							}
							//成品状态高 于新品时  原材料设置为量产发布 否则为新品
							if(finishlevolMax>standardlevol){
								part = (WTPart)WorkflowUtil.setLifeCycleState(part, MASS_RELEASE);
								changeChildState(sheet1map,sheet2map,sheet3map,part);
							}else{
								part = (WTPart)WorkflowUtil.setLifeCycleState(part, NEW_PRODUCT);
								changeChildState(sheet1map,sheet2map,sheet3map,part);
							}
						}
						
					}
				}
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				log.debug("=====================changePartsState Exception 1==========="+e.getMessage());
				e.printStackTrace();
			} catch (WTException e) {
				log.debug("=====================changePartsState Exception 2==========="+e.getMessage());
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		log.debug("=====================changePartsState end===========");
	}
	/**
	 * 设置BOM结构子件状态
	 * @param sheet1map
	 * @param sheet2map
	 * @param sheet3map
	 * @param part
	 */
	public static void changeChildState(Map<String, String> sheet1map,Map<String, String> sheet2map,Map<String, String> sheet3map,WTPart part){
		System.out.println("=======changeChildState start========="+part.getNumber());
		List<WTPart> list = new ArrayList<WTPart>();
		list = BOMHelp.getchildPart(part, list);
		System.out.println("===child size:"+list.size());
		String partstate = part.getState().getState().getDisplay(Locale.CHINA);
		String partstateEN = part.getState().getState().toString();
		String Parentsofttype = "";
		try {
			Parentsofttype = GenericUtil.getTypeId(part);
			System.out.println("=====Parentsofttype：=="+Parentsofttype+"=====partstate："+partstate);

		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (WTException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		for(WTPart childpart : list){
			try {
				String childsofttype = GenericUtil.getTypeId(childpart);
				String childstate = childpart.getState().getState().getDisplay(Locale.CHINA);
				System.out.println("child number:"+childpart.getNumber()+"=====childsofttype：=="+childsofttype+"=====childstate："+childstate);

				//子件为原材料
				if(Parentsofttype.indexOf("cn.tianma.RawMaterial") != -1){
					//父件为原材料
					if(sheet3map.containsKey(partstate) && sheet3map.containsKey(childstate)){
						//父物料优先级
						double statelevol = Double.parseDouble(sheet3map.get(partstate));
						//优先级  取新品的优先级
						double standardlevol = Double.parseDouble(sheet3map.get("新品"));
						
						if(statelevol>standardlevol){
							WorkflowUtil.setLifeCycleState(childpart, MASS_RELEASE);
						}else{
							WorkflowUtil.setLifeCycleState(childpart, NEW_PRODUCT);
						}
					}
					
					
				}else if(Parentsofttype.indexOf("cn.tianma.SemiFinishedGood") != -1 &&  sheet1map.containsKey(partstate)){
					//父件为半成品
					
					//子件为原材料
					if(childsofttype.indexOf("cn.tianma.RawMaterial") != -1){
							//父物料优先级
							double statelevol = Double.parseDouble(sheet1map.get(partstate));
							//优先级  取新品的优先级
							double standardlevol = Double.parseDouble(sheet1map.get("新品"));
							//父件优先级低于新品 设置子件状态为新品，父件优先级高于新品  设置子件状态为量产发布
							
							System.out.println("=====parent:SemiFinishedGood：=="+partstate+"=====child:RawMaterial："+childstate);

							if(statelevol>standardlevol){
								WorkflowUtil.setLifeCycleState(childpart, MASS_RELEASE);
							}else{
								WorkflowUtil.setLifeCycleState(childpart, NEW_PRODUCT);
							}
					}else{
						//子件为半成品
						
						//父物料优先级
						double statelevol = Double.parseDouble(sheet1map.get(partstate));
						System.out.println("=====parent:SemiFinishedGood=="+partstate+"=====child:SemiFinishedGood"+childstate);
						if(sheet1map.containsKey(childstate)){
							//子料优先级
							double standardlevol = Double.parseDouble(sheet1map.get(childstate));
							//子件状态低于父件状态优先级 设置子件为父件状态  否则不变
							if(standardlevol<statelevol){
								WorkflowUtil.setLifeCycleState(childpart, partstateEN);
							}
						}
					}
				}else if(Parentsofttype.indexOf("cn.tianma.FinishedGood") != -1 &&  sheet2map.containsKey(partstate)){
					//父件为成品
					
					//子件为原材料
					if(childsofttype.indexOf("cn.tianma.RawMaterial") != -1){
							//父物料优先级
							double statelevol = Double.parseDouble(sheet2map.get(partstate));
							//优先级  取新品的优先级
							double standardlevol = Double.parseDouble(sheet2map.get("新品"));
							//父件优先级低于新品 设置子件状态为新品，父件优先级高于新品  设置子件状态为量产发布
							if(statelevol>standardlevol){
								WorkflowUtil.setLifeCycleState(childpart, MASS_RELEASE);
							}else{
								WorkflowUtil.setLifeCycleState(childpart, NEW_PRODUCT);
							}
					}else{
						//子件为半成品 或成品
						
						//父物料优先级
						double statelevol = Double.parseDouble(sheet1map.get(partstate));
						if(sheet1map.containsKey(childstate)){
							//子料优先级
							double standardlevol = Double.parseDouble(sheet1map.get(childstate));
							//子件状态低于父件状态优先级 设置子件为父件状态  否则不变
							if(standardlevol<statelevol){
								WorkflowUtil.setLifeCycleState(childpart, partstateEN);
							}
						}
					}
				}
				
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WTException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		System.out.println("=======changeChildState end=========");

	}
	
	/**
	 * 针对受影响对象做处理
	 * @param part
	 * @param state
	 * @param map
	 */
	public static void changePartState(WTPart part,String enstate,Map<String, Map<String, String>> map) {
		log.debug("=====================changePartState start===========");
		
		//先取Excel中 不同类型对应的状态及优先级
		//半成品
		Map<String, String> sheet1map = new HashMap<String, String>();
		//成品
		Map<String, String> sheet2map = new HashMap<String, String>();
		//原材料
		Map<String, String> sheet3map = new HashMap<String, String>();
		Iterator iter = map.keySet().iterator();
		while(iter.hasNext()){
			String key = (String)iter.next();
			Map<String, String> sheet = map.get(key);
			log.debug("key:"+key);
			log.debug("value:"+sheet.toString());
			if("sheet1".equals(key)){
				sheet1map = map.get(key);
			}else if("sheet2".equals(key)){
				sheet2map = map.get(key);
			}else if("sheet3".equals(key)){
				sheet3map = map.get(key);
			}
		}
			try {
				String softtype = GenericUtil.getTypeId(part);
				String partstate = part.getState().getState().toString();
				
				log.debug("变更后状态："+partstate);
				log.debug("变更前状态："+enstate);
				//if(softtype.indexOf("cn.tianma.FinishedGood") != -1){
					//成品
					if(!partstate.equals(enstate)){
						log.debug("============= not equals==========");
						WorkflowUtil.setLifeCycleState(part, enstate);
					}
					
				/*}else if(softtype.indexOf("cn.tianma.SemiFinishedGood") != -1){
					log.debug("=============SemiFinishedGood ==========");
					//半成品
					if(sheet1map.containsKey(partstate)){
						log.debug("=============SemiFinishedGood containsKey==========");
						//优先级
						double standardlevol = Double.parseDouble(sheet1map.get("新品"));
						//当前物料优先级
						double statelevol = Double.parseDouble(sheet1map.get(partstate));
						log.debug("=============SemiFinishedGood containsKey===standardlevol:"+standardlevol+"===statelevol:"+statelevol);
						log.debug("=============SemiFinishedGood statelevol<=standardlevol:"+(statelevol<=standardlevol));
						if(statelevol<=standardlevol){
							WorkflowUtil.setLifeCycleState(part, NEW_PRODUCT);
						}else{
							//比较成品状态  再 设置
							
						}
						
					}
				}else if(softtype.indexOf("cn.tianma.RawMaterial") != -1){
					log.debug("=============RawMaterial==========");
					//原材料
					//优先级
					double standardlevol = Double.parseDouble(sheet1map.get("新品"));
					//当前物料优先级
					double statelevol = Double.parseDouble(sheet1map.get(partstate));
					log.debug("=============RawMaterial containsKey===standardlevol:"+standardlevol+"===statelevol:"+statelevol);

					if(statelevol<=standardlevol){
						log.debug("=============RawMaterial  111  statelevol<=standardlevol:"+(statelevol<=standardlevol));

						//正在工作、新品时
						WorkflowUtil.setLifeCycleState(part, NEW_PRODUCT);
					}else{
						log.debug("=============RawMaterial 222 statelevol<=standardlevol:"+(statelevol<=standardlevol));
						//非正在工作、新品时  设置为  量产发布
						WorkflowUtil.setLifeCycleState(part, MASS_RELEASE);
					}
					
				}*/
					
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.debug(e.getMessage());
			} catch (WTException e) {
				log.debug("=====================changePartState Exception==========="+e.getMessage());
				e.printStackTrace();
			}
			log.debug("=====================changePartState end===========");
	}

	public static Map<String, Map<String, String>> readExcel() {
		log.debug("=====================ECOCRUDUtil  readExcel start===========");
		// key:类型 value 类型对应的状态及优先级
		Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
		// 半成品
		Map<String, String> stateList1 = new HashMap<String, String>();
		// 成品
		Map<String, String> stateList2 = new HashMap<String, String>();
		// 原材料
		Map<String, String> stateList3 = new HashMap<String, String>();
		log.debug("开始state=====>>>");
		InputStream is = null;
		Workbook wb = null;
		String state = "";// 状态
		String levol = "";// 优先级
		try {
			is = new FileInputStream(tempFolderPath);
			//is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\ecostate.xlsx");
			wb = new XSSFWorkbook(is);
			Sheet sheet1 = wb.getSheet("半成品");
			Sheet sheet2 = wb.getSheet("成品");
			Sheet sheet3 = wb.getSheet("原材料");
			Row row;
			Cell cell;

			//半成品   第一行为表头，从第二行开始遍历
			for (int i = 1, rowNum = sheet1.getLastRowNum(); i <= rowNum; i++) {
				if ((row = sheet1.getRow(i)) != null) {
					cell = row.getCell(1);
					if(cell!=null){
						state = getCellValueAsString(cell);
					}
					cell = row.getCell(2);
					if(cell != null){
						levol = getCellValueAsString(cell);
						log.debug("1123---->>>111111:::" + levol);
					}
					if (!"".equals(state) && !"".equals(levol)) {
						stateList1.put(state, levol);
					}
				}
			}

			//成品   第一行为表头，从第二行开始遍历
			for (int i = 1, rowNum = sheet2.getLastRowNum(); i <= rowNum; i++) {
				if ((row = sheet2.getRow(i)) != null) {
					cell = row.getCell(1);
					if(cell!=null){
						state = getCellValueAsString(cell);
					}
					cell = row.getCell(2);
					if(cell != null){
						levol = getCellValueAsString(cell);
						log.debug("1123---->>>22222222:::" + levol);
					}
					
					if (state != null && levol != null) {
						stateList2.put(state, levol);
					}
				}
			}

			//原材料   第一行为表头，从第二行开始遍历
			for (int i = 1, rowNum = sheet3.getLastRowNum(); i <= rowNum; i++) {
				if ((row = sheet3.getRow(i)) != null) {
					cell = row.getCell(1);
					if(cell!=null){
						state = getCellValueAsString(cell);

					}
					cell = row.getCell(2);
					if(cell != null){
						levol = getCellValueAsString(cell);
						log.debug("1123---->>>3333333333:::" + levol);
					}
					if (state != null && levol != null) {
						stateList3.put(state, levol);
					}
				}
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map.put("sheet1", stateList1);
		map.put("sheet2", stateList2);
		map.put("sheet3", stateList3);
		log.debug("=====================ECOCRUDUtil  readExcel end==========="+map.toString());
		return map;
	}

	/**
	 * 审核通过后 更新ECO的状态为 open
	 * 
	 * @param econumber
	 * @return
	 */
	public static boolean commitECOData(String econumber) {
		boolean result = false;
		WTConnection wtConn = null;
		MethodContext methodcontext = MethodContext.getContext();
		List<ECOBean> list = new ArrayList<ECOBean>();
		// Transaction trans = new Transaction();
		try {
			// trans.start();
			wtConn = (WTConnection) methodcontext.getConnection();
			String SQL = "update TMECOITEMModel set ECOStatus = 'close' where ECONumber = ? ";
			int rs = 0;
			PreparedStatement pstmt = null;
			pstmt = wtConn.prepareStatement(SQL);
			pstmt.setString(1, econumber);
			rs = pstmt.executeUpdate();
			log.debug("=================rs==========" + rs);
			if (rs != 0) {
				result = true;
			}
			pstmt.close();
			pstmt = null;
			wtConn.release();
			wtConn = null;
			// trans.commit();
			// trans = null;

		} catch (Exception e1) {
			// trans.rollback();
			result = false;
			e1.printStackTrace();
			log.debug("===========update failed=====" + e1);
		} finally {
			if (wtConn != null) {
				wtConn.release();
			}
		}
		return result;
	}

	/**
	 * 查询条件：ECO编号；,状态为：open
	 * 
	 * @param wtConn
	 * @param bean
	 * @return
	 */
	public static List<BOMBean> queryECOByNumber(String number) {
		boolean result = false;
		WTConnection wtConn = null;
		MethodContext methodcontext = MethodContext.getContext();
		List<BOMBean> list = new ArrayList<BOMBean>();
		// String querySQL =
		// "select * from TMECOITEMModel where ECONumber like ? or ECOPARTNUMBER
		// like ? or ECOView like ? or ECOOperatingType like ? ";
		String querySQL = "select * from TMECOITEMModel where ECONumber =  ? and ECOStatus = 'open'";
		String sName = "";
		try {
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			wtConn = (WTConnection) methodcontext.getConnection();
			pstmt = wtConn.prepareStatement(querySQL);
			pstmt.setString(1, number);
			log.debug("=======queryECOByNumber querySQL======" + querySQL);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				BOMBean bean = new BOMBean();
				// 变更类型
				String type = rs.getString("ECOOperatingType");
				log.debug("========ECOOperatingType:" + type);
				// ECO单号
				String ECONumber = rs.getString("ECONumber");
				bean.setECONumber(ECONumber);
				// 成品号
				String effectnumber = rs.getString("str1");
				bean.setEffectnumber(effectnumber);
				// 视图
				String view = rs.getString("ECOView");
				bean.setView(view);
				// 物料编号
				String partnumber = rs.getString("ECOPARTNUMBER");
				log.debug("========ECOPARTNUMBER:" + partnumber + "=====str1:" + effectnumber);
				// 用量
				String amount = rs.getString("ECODosage");
				// 行号
				String linenumber = rs.getString("ECOLineNumber");
				// 新物料号
				String newpartnumber = rs.getString("ECONewPartNumber");
				// 新物料用量
				String newamount = rs.getString("ECONewDosage");
				// 新物料行号
				String newlinenumber = rs.getString("ECONewLineNumber");
				// 替换物料号
				String replacepartnumber = rs.getString("ECOSubstituteNumber");
				// 替代料行号
				String replacelinenumber = rs.getString("ECOSubstituteLineNmuber");
				// 替代料用量
				String replaceamount = rs.getString("ECOSubstituteDosage");
				// 新替代料号
				String newreplacenumber = rs.getString("ECONewSubstituteNumber");
				// 新替代料行号
				String newreplacelinenumber = rs.getString("ECONewSubstituteLineNmuber");
				// 新替代料用量
				String newreplaceamount = rs.getString("ECONewSubstituteDosage");
				// bean.setECOView(view);
				if (BOMBean.ADDPart.equals(type)) {
					bean.setType(BOMBean.ADDPart);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
				} else if (BOMBean.DelPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setView(view);
					bean.setOldpartlinenumber(linenumber);
					bean.setType(BOMBean.DelPart);
				} else if (BOMBean.ReplacePart.equals(type)) {
					bean.setType(BOMBean.ReplacePart);
					bean.setNewpartlinenumber(newlinenumber);
					bean.setNewpartamount(newamount);
					bean.setNewpartnumber(newpartnumber);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartlinenumber(linenumber);
					bean.setOldpartamount(amount);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
				} else if (BOMBean.UpdatePart.equals(type)) {
					bean.setType(BOMBean.UpdatePart);
					bean.setNewpartamount(newamount);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setOldpartlinenumber(linenumber);
					bean.setView(view);
					bean.setEffectnumber(effectnumber);
				} else if (BOMBean.ADDReplcePart.equals(type)) {
					bean.setType(BOMBean.ADDReplcePart);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setPartBnumber(replacepartnumber);
					bean.setPartBlinenumber(replacelinenumber);
					bean.setPartBamount(replaceamount);
				} else if (BOMBean.DelReplcePart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setPartBnumber(replacepartnumber);
					bean.setType(BOMBean.DelReplcePart);
				} else if (BOMBean.UpdateReplcePart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.UpdateReplcePart);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setOldpartnumber(replacepartnumber);
					bean.setOldpartamount(replaceamount);
					bean.setNewpartnumber(newreplacenumber);
					bean.setNewpartamount(newreplaceamount);

				} else if (BOMBean.UpdateAPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.UpdateAPart);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setOldpartlinenumber(linenumber);
					bean.setNewpartnumber(newpartnumber);
					bean.setNewpartamount(newamount);

				} else if (BOMBean.ReplaceABPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.ReplaceABPart);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartlinenumber(linenumber);
					bean.setNewpartnumber(newpartnumber);

				}
				//add by xucr on 20210120 添加新的菜单：替代料用量修改
				else if(BOMBean.UpdateSubstitutesQuantity.equals(type)) {
					bean.setType(BOMBean.UpdateSubstitutesQuantity);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setMainNum(partnumber);//主料
					bean.setSubstitutesNum(replacepartnumber);//存放替代料
					bean.setSubNewAmount(newreplaceamount);//替代料新用量
				}
				list.add(bean);
				result = true;
			}
			log.debug("queryECOByNumber result list:" + list.size());
			rs.close();
			rs = null;

			pstmt.close();
			pstmt = null;
			wtConn.release();
			wtConn = null;
			// result = true;
		} catch (Exception e) {
			result = false;
		} finally {
			if (wtConn != null) {
				wtConn.release();
			}
		}
		return list;
	}

	/**
	 * 查询条件：物料编号； 查询是否存在于open的ECO编号
	 * 
	 * @param wtConn
	 * @param bean
	 * @return
	 */
	public static List<String> queryHistoryByNumber(String partnumber, String econumber) {
		MethodContext context = MethodContext.getContext();
		WTConnection conn = null;
		boolean result = false;
		List<String> list = new ArrayList<String>();
		// String querySQL =
		// "select * from TMECOITEMModel where ECONumber like ? or ECOPARTNUMBER
		// like ? or ECOView like ? or ECOOperatingType like ? ";
		String querySQL = "select * from TMECOITEMModel where str1 = ? and ECOStatus = 'open'";
		String sName = "";
		try {
			conn = (WTConnection) context.getConnection();
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			pstmt = conn.prepareStatement(querySQL);
			// pstmt.setString(1, econumber);
			pstmt.setString(1, partnumber);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				// ECO单号
				String ECONumber = rs.getString("ECONumber");
				if (!econumber.equals(ECONumber) && !list.contains(ECONumber)) {
					list.add(ECONumber);
				}
				/*
				 * BOMBean bean = new BOMBean(); // 变更类型 String type =
				 * rs.getString("ECOOperatingType");
				 * 
				 * bean.setECONumber(ECONumber); // 成品号 String ECOPARTNUMBER =
				 * rs.getString("str1"); //
				 * bean.setECOPartNumber(ECOPARTNUMBER); // 物料编号 String
				 * effectnumber = rs.getString("ECOPARTNUMBER"); //
				 * bean.setECOAffectnumber(effectnumber); // 视图 String view =
				 * rs.getString("ECOView"); // bean.setECOView(view);
				 * list.add(bean);
				 */
				result = true;
			}
			rs.close();
			rs = null;

			pstmt.close();
			pstmt = null;
			conn.release();
			conn = null;
		} catch (Exception e) {
			result = false;
		} finally {
			if (conn != null) {
				conn.release();
			}
		}
		return list;
	}

	/**
	 * 查询条件：ECO编号；
	 * 
	 * @param wtConn
	 * @param bean
	 * @return
	 */
	public static List<BOMBean> queryECOByECONumber(String number) {
		boolean result = false;
		List<BOMBean> list = new ArrayList<BOMBean>();
		MethodContext context = MethodContext.getContext();
		WTConnection conn = null;
		Transaction trans = new Transaction();
		// String querySQL =
		// "select * from TMECOITEMModel where ECONumber like ? or ECOPARTNUMBER
		// like ? or ECOView like ? or ECOOperatingType like ? ";
		String querySQL = "select * from TMECOITEMModel where ECONumber = ? ";
		String sName = "";
		try {
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			conn = (WTConnection) context.getConnection();
			pstmt = conn.prepareStatement(querySQL);
			pstmt.setString(1, number);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				BOMBean bean = new BOMBean();
				// 变更类型
				String type = rs.getString("ECOOperatingType");
				log.debug("========ECOOperatingType:" + type);
				// ECO单号
				String ECONumber = rs.getString("ECONumber");
				bean.setECONumber(ECONumber);
				// 成品号
				String effectnumber = rs.getString("str1");
				bean.setEffectnumber(effectnumber);
				// 视图
				String view = rs.getString("ECOView");
				bean.setView(view);
				// 物料编号
				String partnumber = rs.getString("ECOPARTNUMBER");
				log.debug("========ECOPARTNUMBER:" + partnumber + "=====str1:" + effectnumber);
				// 用量
				String amount = rs.getString("ECODosage");
				// 行号
				String linenumber = rs.getString("ECOLineNumber");
				// 新物料号
				String newpartnumber = rs.getString("ECONewPartNumber");
				// 新物料用量
				String newamount = rs.getString("ECONewDosage");
				// 新物料行号
				String newlinenumber = rs.getString("ECONewLineNumber");
				// 替换物料号
				String replacepartnumber = rs.getString("ECOSubstituteNumber");
				// 替代料行号
				String replacelinenumber = rs.getString("ECOSubstituteLineNmuber");
				// 替代料用量
				String replaceamount = rs.getString("ECOSubstituteDosage");
				// 新替代料号
				String newreplacenumber = rs.getString("ECONewSubstituteNumber");
				// 新替代料行号
				String newreplacelinenumber = rs.getString("ECONewSubstituteLineNmuber");
				// 新替代料用量
				String newreplaceamount = rs.getString("ECONewSubstituteDosage");
				// bean.setECOView(view);
				if (BOMBean.ADDPart.equals(type)) {
					bean.setType(BOMBean.ADDPart);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
				} else if (BOMBean.DelPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setView(view);
					bean.setOldpartlinenumber(linenumber);
					bean.setType(BOMBean.DelPart);
				} else if (BOMBean.ReplacePart.equals(type)) {
					bean.setType(BOMBean.ReplacePart);
					bean.setNewpartlinenumber(newlinenumber);
					bean.setNewpartamount(newamount);
					bean.setNewpartnumber(newpartnumber);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartlinenumber(linenumber);
					bean.setOldpartamount(amount);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
				} else if (BOMBean.UpdatePart.equals(type)) {
					bean.setType(BOMBean.UpdatePart);
					bean.setNewpartamount(newamount);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setOldpartlinenumber(linenumber);
					bean.setView(view);
					bean.setEffectnumber(effectnumber);
				} else if (BOMBean.ADDReplcePart.equals(type)) {
					bean.setType(BOMBean.ADDReplcePart);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setPartBnumber(replacepartnumber);
					bean.setPartBlinenumber(replacelinenumber);
					bean.setPartBamount(replaceamount);
				} else if (BOMBean.DelReplcePart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setPartBnumber(replacepartnumber);
					bean.setType(BOMBean.DelReplcePart);
				} else if (BOMBean.UpdateReplcePart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.UpdateReplcePart);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setOldpartnumber(replacepartnumber);
					bean.setOldpartamount(replaceamount);
					bean.setNewpartnumber(newreplacenumber);
					bean.setNewpartamount(newreplaceamount);

				} else if (BOMBean.UpdateAPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.UpdateAPart);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setNewpartnumber(newpartnumber);
					bean.setNewpartamount(newamount);

				} else if (BOMBean.ReplaceABPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.ReplaceABPart);
					bean.setOldpartnumber(partnumber);
					bean.setNewpartnumber(newpartnumber);

				}//add by xucr on 20210120 添加新的菜单：替代料用量修改
				else if(BOMBean.UpdateSubstitutesQuantity.equals(type)) {
					bean.setType(BOMBean.UpdateSubstitutesQuantity);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setPartAnumber(partnumber);//主料
					bean.setPartBnumber(replacepartnumber);//存放替代料
					bean.setPartBamount(newreplaceamount);//替代料新用量
				}
				list.add(bean);
				result = true;
			}
			rs.close();
			rs = null;

			pstmt.close();
			pstmt = null;
			conn.release();
			conn = null;
			trans.commit();
			// result = true;
		} catch (Exception e) {
			result = false;
		} finally {
			if (conn != null) {
				conn.release();
			}
		}
		return list;
	}

	/**
	 * 根据名称获取存储库
	 */
	public static WTContainer getContainerByName(String name) throws WTException {

		QuerySpec queryspec = new QuerySpec(WTContainer.class);
		queryspec.appendWhere(new SearchCondition(WTContainer.class, WTContainer.NAME, SearchCondition.EQUAL, name), 0);
		QueryResult queryresult = PersistenceHelper.manager.find(queryspec);
		if (queryresult.hasMoreElements()) {
			WTContainer container = (WTContainer) queryresult.nextElement();
			// WTContainerRef wtcontainerref1 =
			// pdmlinkproduct.getContainerReference();
			return container;
		}
		return null;

	}

	/**
	 * 查询条件：ECO编号； 返回本次单个变更的 受影响对象
	 * 
	 * @param wtConn
	 * @param bean
	 * @return
	 */
	public static List<BOMBean> queryEffectPartByECONumber(String number, String type) {
		List<BOMBean> list = new ArrayList<BOMBean>();
		MethodContext context = MethodContext.getContext();
		WTConnection conn = null;
		// --根据ECO编号 查找受影响对象记录
		String querySQL = "select distinct str1,ecoview from TMECOITEMModel a where econumber = ? ";
		if (Zero.equals(type)) {
			querySQL += " and str2 = '0' ";
		}
		String sName = "";
		try {
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			conn = (WTConnection) context.getConnection();
			pstmt = conn.prepareStatement(querySQL);
			pstmt.setString(1, number);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				BOMBean bean = new BOMBean();
				// 受影响对象编号
				String effectnumber = rs.getString("str1");
				bean.setEffectnumber(effectnumber);
				// 视图
				String view = rs.getString("ECOView");
				bean.setView(view);
				list.add(bean);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			conn.release();
			conn = null;
		} catch (Exception e) {
			log.debug("========查找受影响对象失败=======" + e);

		} finally {
			if (conn != null) {
				conn.release();
			}
		}
		return list;
	}

	/**
	 * 根据ECO编号、受影响对象编号、变更类型 查找记录 用于单个变更
	 * 
	 * --根据ECO编号、变更类型 查找记录 用于批量变更
	 * 
	 * @param econumber
	 *            ECO编号
	 * @param number
	 *            受影响对象编号
	 * @param operatetype
	 *            变更类型
	 * @param type
	 *            用于区别单个变更(0)还是批量变更(1)
	 * @return
	 */
	public static List<BOMBean> queryECOByNumberAndType(String econumber, String number, String operatetype,
			String types, String views) {
		log.debug("=======queryECOByNumberAndType querySQL======econumber：" + econumber + "," + operatetype
				+ "," + types + "," + number + "," + views);
		boolean result = false;
		WTConnection wtConn = null;
		MethodContext methodcontext = MethodContext.getContext();
		List<BOMBean> list = new ArrayList<BOMBean>();
		String querySQL = "select * from TMECOITEMModel where econumber = ? and ecooperatingtype = ? ";
		// ECO单个变更类型查询
		if (Zero.equals(types)) {
			querySQL += " and str1 = ? and ecoview = ? and str2 = '0' ";
		} else if (First.equals(types)) {
			// ECO 批量变更查询
			querySQL += "and str2 = '1' ";
		} else {
			// ECO 变更查询
			querySQL += " and str1 = ? and ecoview = ? ";
		}
		try {
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			wtConn = (WTConnection) methodcontext.getConnection();
			pstmt = wtConn.prepareStatement(querySQL);
			pstmt.setString(1, econumber);
			pstmt.setString(2, operatetype);
			if (!First.equals(types)) {
				pstmt.setString(3, number);
				pstmt.setString(4, views);
			}

			log.debug("=======queryECOByNumberAndType querySQL======" + querySQL);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				BOMBean bean = new BOMBean();
				// 变更类型
				String type = rs.getString("ECOOperatingType");
				log.debug("========ECOOperatingType:" + type);
				// ECO单号
				String ECONumber = rs.getString("ECONumber");
				bean.setECONumber(ECONumber);
				// 成品号
				String effectnumber = rs.getString("str1");
				bean.setEffectnumber(effectnumber);
				// 视图
				String view = rs.getString("ECOView");
				bean.setView(view);
				// 物料编号
				String partnumber = rs.getString("ECOPARTNUMBER");
				log.debug("========ECOPARTNUMBER:" + partnumber + "=====str1:" + effectnumber);
				// 用量
				String amount = rs.getString("ECODosage");
				// 行号
				String linenumber = rs.getString("ECOLineNumber");
				// 新物料号
				String newpartnumber = rs.getString("ECONewPartNumber");
				// 新物料用量
				String newamount = rs.getString("ECONewDosage");
				// 新物料行号
				String newlinenumber = rs.getString("ECONewLineNumber");
				// 替换物料号
				String replacepartnumber = rs.getString("ECOSubstituteNumber");
				// 替代料行号
				String replacelinenumber = rs.getString("ECOSubstituteLineNmuber");
				// 替代料用量
				String replaceamount = rs.getString("ECOSubstituteDosage");
				// 新替代料号
				String newreplacenumber = rs.getString("ECONewSubstituteNumber");
				// 新替代料行号
				String newreplacelinenumber = rs.getString("ECONewSubstituteLineNmuber");
				// 新替代料用量
				String newreplaceamount = rs.getString("ECONewSubstituteDosage");
				// bean.setECOView(view);
				if (BOMBean.ADDPart.equals(type)) {
					bean.setType(BOMBean.ADDPart);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
				} else if (BOMBean.DelPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setView(view);
					bean.setOldpartlinenumber(linenumber);
					bean.setType(BOMBean.DelPart);
				} else if (BOMBean.ReplacePart.equals(type)) {
					bean.setType(BOMBean.ReplacePart);
					bean.setNewpartlinenumber(newlinenumber);
					bean.setNewpartamount(newamount);
					bean.setNewpartnumber(newpartnumber);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartlinenumber(linenumber);
					bean.setOldpartamount(amount);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
				} else if (BOMBean.UpdatePart.equals(type)) {
					bean.setType(BOMBean.UpdatePart);
					bean.setNewpartamount(newamount);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setOldpartlinenumber(linenumber);
					bean.setView(view);
					bean.setEffectnumber(effectnumber);
				} else if (BOMBean.ADDReplcePart.equals(type)) {
					bean.setType(BOMBean.ADDReplcePart);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setPartBnumber(replacepartnumber);
					bean.setPartBlinenumber(replacelinenumber);
					bean.setPartBamount(replaceamount);
				} else if (BOMBean.DelReplcePart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setPartBnumber(replacepartnumber);
					bean.setType(BOMBean.DelReplcePart);
				} else if (BOMBean.UpdateReplcePart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.UpdateReplcePart);
					bean.setPartAnumber(partnumber);
					bean.setPartAlinenumber(linenumber);
					bean.setOldpartnumber(replacepartnumber);
					bean.setOldpartamount(replaceamount);
					bean.setNewpartnumber(newreplacenumber);
					bean.setNewpartamount(newreplaceamount);

				} else if (BOMBean.UpdateAPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.UpdateAPart);
					bean.setOldpartnumber(partnumber);
					bean.setOldpartamount(amount);
					bean.setOldpartlinenumber(linenumber);
					bean.setNewpartnumber(newpartnumber);
					bean.setNewpartamount(newamount);

				} else if (BOMBean.ReplaceABPart.equals(type)) {
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setType(BOMBean.ReplaceABPart);
					bean.setOldpartnumber(partnumber);
					bean.setNewpartnumber(newpartnumber);
					bean.setOldpartlinenumber(linenumber);
				}
				//add by xucr on 20210120 添加新的菜单：替代料用量修改
				else if(BOMBean.UpdateSubstitutesQuantity.equals(type)) {
					bean.setType(BOMBean.UpdateSubstitutesQuantity);
					bean.setEffectnumber(effectnumber);
					bean.setView(view);
					bean.setMainNum(partnumber);//主料
					bean.setSubstitutesNum(replacepartnumber);//存放替代料
					bean.setSubNewAmount(newreplaceamount);//替代料新用量
					bean.setSubOldAmount(replaceamount);//替代料旧用量
					bean.setMainLineNumber(linenumber);//行号
				}
				list.add(bean);
				result = true;
			}
			log.debug("queryECOByNumberAndType result list:" + list.size());
			rs.close();
			rs = null;

			pstmt.close();
			pstmt = null;
			wtConn.release();
			wtConn = null;
			// result = true;
		} catch (Exception e) {
			result = false;
		} finally {
			if (wtConn != null) {
				wtConn.release();
			}
		}
		return list;
	}

	/**
	 * 删除 ECO清单中指定的受影响对象
	 * 
	 * @param econumber
	 * @return
	 */
	public static boolean deleteECO(String econumber, String effectnumber, String ecooperatingtype, String type) {
		boolean result = false;
		WTConnection wtConn = null;
		MethodContext methodcontext = MethodContext.getContext();
		try {
			wtConn = (WTConnection) methodcontext.getConnection();
			String SQL = "delete TMECOITEMModel where ECONumber = ? and ecooperatingtype = ? ";
			if (Zero.equals(type)) {
				SQL += "and str1 = ? and str2 = '0' ";
			} else {
				SQL += "and str2 = '1' ";
			}
			int rs = 0;
			PreparedStatement pstmt = null;
			pstmt = wtConn.prepareStatement(SQL);
			pstmt.setString(1, econumber);
			pstmt.setString(2, ecooperatingtype);
			if (Zero.equals(type)) {
				pstmt.setString(3, effectnumber);
			}
			rs = pstmt.executeUpdate();
			log.debug("=================rs==========" + rs);
			if (rs != 0) {
				result = true;
			}
			pstmt.close();
			pstmt = null;
			wtConn.release();
			wtConn = null;
			log.debug("===========deleteECOData success=====");
		} catch (Exception e1) {
			result = false;
			e1.printStackTrace();
			log.debug("===========deleteECOData failed=====" + e1);
		} finally {
			if (wtConn != null) {
				wtConn.release();
			}
		}
		return result;
	}

	/**
	 * 删除 ECO清单中指定的受影响对象
	 * 
	 * @param econumber
	 * @return
	 */
	public static boolean deleteECOData(String econumber, String effectnumber, String view) {
		boolean result = false;
		WTConnection wtConn = null;
		MethodContext methodcontext = MethodContext.getContext();
		try {
			wtConn = (WTConnection) methodcontext.getConnection();
			String SQL = "delete TMECOITEMModel where ECONumber = ? and str1 = ? and ecoview = ? ";
			int rs = 0;
			PreparedStatement pstmt = null;
			pstmt = wtConn.prepareStatement(SQL);
			pstmt.setString(1, econumber);
			pstmt.setString(2, effectnumber);
			pstmt.setString(3, view);
			rs = pstmt.executeUpdate();
			log.debug("=================rs==========" + rs);
			if (rs != 0) {
				result = true;
			}
			pstmt.close();
			pstmt = null;
			wtConn.release();
			wtConn = null;
			log.debug("===========deleteECOData success=====");
		} catch (Exception e1) {
			result = false;
			e1.printStackTrace();
			log.debug("===========deleteECOData failed=====" + e1);
		} finally {
			if (wtConn != null) {
				wtConn.release();
			}
		}
		return result;
	}

	/**
	 * 根据ECO编号查找本次变更的类型
	 * 
	 * @param econumber
	 * @return
	 */
	public static String queryECOtype(String econumber, String effectnumber, String view) {
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		WTConnection wtConn = null;
		MethodContext methodcontext = MethodContext.getContext();
		try {
			wtConn = (WTConnection) methodcontext.getConnection();
			String SQL = "select ecooperatingtype ,count(*) from tmecoitemmodel where " + "econumber = ? "
					+ "and str1 = ? " + "and ECOView = ? " + "group by ecooperatingtype";
			ResultSet rs = null;
			PreparedStatement pstmt = null;
			pstmt = wtConn.prepareStatement(SQL);
			log.debug("====queryECOtype sql:" + SQL);
			pstmt.setString(1, econumber);
			pstmt.setString(2, effectnumber);
			pstmt.setString(3, view);
			rs = pstmt.executeQuery();
			log.debug("=================rs==========" + rs);
			while (rs.next()) {
				JSONObject obj = new JSONObject();
				String type = rs.getString("ecooperatingtype");
				String size = rs.getString(2);
				obj.put("type", type);
				obj.put("size", size);
				array.put(obj);
			}
			pstmt.close();
			pstmt = null;
			wtConn.release();
			wtConn = null;
			log.debug("===========queryECOtype success=====" + array.toString());
		} catch (Exception e1) {
			e1.printStackTrace();
			log.debug("===========queryECOtype failed=====" + e1);
		} finally {
			if (wtConn != null) {
				wtConn.release();
			}
		}
		try {
			object.put("datalist", array);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object.toString();
	}
	
	private static String parseFormula(String pPOIFormula) {
		final String cstReplaceString = "ATTR(semiVolatile)";
		StringBuffer result = null;
		int index;

		result = new StringBuffer();
		index = pPOIFormula.indexOf(cstReplaceString);
		if (index >= 0) {
			result.append(pPOIFormula.substring(0, index));
			result.append(pPOIFormula.substring(index + cstReplaceString.length()));
		} else {
			result.append(pPOIFormula);
		}

		return result.toString();
	}
	
	/**
	 * 获取单元格的值
	 * 
	 * @param cell
	 * @return
	 */
	public static String getCellValueAsString(Cell cell){
		String cellStringValue = "" ;
		
		if(cell == null){
			return cellStringValue ;
		}
		
		int cellType = cell.getCellType();
		
		switch (cellType) {
			case Cell.CELL_TYPE_BOOLEAN :
			{	
				cellStringValue = cell.getBooleanCellValue() + "" ;
				break;
			}
			case Cell.CELL_TYPE_ERROR:
			{
				cellStringValue = cell.getErrorCellValue() + "";
				break;
			}
			case Cell.CELL_TYPE_FORMULA:
			{
				cellStringValue = parseFormula(cell.getCellFormula());
				break;
			}
			case Cell.CELL_TYPE_NUMERIC:
			{	
				cellStringValue = cell.getNumericCellValue() + "";
				break;
			}
			case Cell.CELL_TYPE_STRING:
			{
				cellStringValue = cell.getStringCellValue();
				break;
			}
		}
		
		return cellStringValue ;
	}
	

	public static void main(String[] args) {
		String s = "1.5";
		long l = Float.valueOf(s).longValue();
		log.debug(l);
	}
	/**
	 * 选择筛选条件：成品编号 str1
	 * 
	 * 创建时间 createstampa2
	 * 
	 * ECOOperatingType
	 * 
	 * @param wtConn
	 * @param bean
	 * @return
	 */
	public static List<ECOBean> searchECOData(String partNumber) {
		MethodContext context = MethodContext.getContext();
		WTConnection conn = null;
		Transaction trans = new Transaction();
		boolean result = false;
		List<ECOBean> list = new ArrayList<ECOBean>();
		List<String> listStr1 = new ArrayList<String>();
		StringBuffer sb = new StringBuffer();
		sb.append("select distinct econumber,createstampa2,ecoview from TMECOITEMModel");
		if (!BOMHelp.isNullOrEmpty(partNumber)) {
			sb.append(" where str1 ="+"'"+ partNumber + "'");
		}

		try {
			conn = (WTConnection) context.getConnection();
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			pstmt = conn.prepareStatement(sb.toString());
			if (!BOMHelp.isNullOrEmpty(partNumber)) {
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				ECOBean bean = new ECOBean();
				String ECONumber = rs.getString("ECONumber");
				// ECO单号
				bean.setECONumber(ECONumber);
				// 创建时间
				Timestamp createtime = rs.getTimestamp("createstampa2");
				bean.setCreatetime(IBAUtil.transfTime(createtime));
				//视图
				String ECOView = rs.getString("ECOView");
				bean.setECOView(ECOView);
				if (!listStr1.contains(ECONumber)) {
					list.add(bean);
					listStr1.add(ECONumber);
				}
				result = true;
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			conn.release();
			conn = null;
		} catch (Exception e) {
			result = false;
		} finally {
			if (conn != null) {
				conn.release();
			}
		}
		return list;
	}
	/**
	 * 删除 批量变更中 某一类型 的数据
	 * 
	 * @param econumber
	 * @return
	 */
	public static boolean deleteECOChangeType1(String econumber, String ecooperatingtype) {
		boolean result = false;
		WTConnection wtConn = null;
		MethodContext methodcontext = MethodContext.getContext();
		try {
			wtConn = (WTConnection) methodcontext.getConnection();
			String SQL = "delete TMECOITEMModel where ECONumber = ? and str2 = '1' and  ecooperatingtype = ? ";
			int rs = 0;
			PreparedStatement pstmt = null;
			pstmt = wtConn.prepareStatement(SQL);
			pstmt.setString(1, econumber);
			pstmt.setString(2, ecooperatingtype);
			rs = pstmt.executeUpdate();
			log.debug("=================rs==========" + rs);
			if (rs != 0) {
				result = true;
			}
			pstmt.close();
			pstmt = null;
			wtConn.release();
			wtConn = null;
			log.debug("===========deleteECOData success=====");
		} catch (Exception e1) {
			result = false;
			e1.printStackTrace();
			log.debug("===========deleteECOData failed=====" + e1);
		} finally {
			if (wtConn != null) {
				wtConn.release();
			}
		}
		return result;
	}
	
	public static String OBSOLESCENCEECO(String oid) {
		/**
		 * add by xucr on 20210126
		 * 此方法是从正式环境class反编译后加入的，
		 * 可能是提交者只发布了class，并未提交源文件
		 * */
		boolean flag = false;
	    String result = "";
	    WTChangeOrder2 eco = null;
	    try {
	    	eco = (WTChangeOrder2)BOMHelp.REF_FACTORY.getReference(oid).getObject();
	    } catch (Exception e) {
	    	result = result + e.getMessage();
	    	flag = false;
	    	e.printStackTrace();
	    }
	    finally {
	    	if (eco != null) {
	    		flag = commitECOData(eco.getNumber());
	    	}
	    }
	    return result;
	}
}
